﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="AnagraficaCodiciFornitori.aspx.cs" Inherits="FimetContoDeposito.Anagrafiche.AnagraficaCodiciFornitori" %>

<%@ Register assembly="GrayParrot.Web.Controls" namespace="GrayParrot.Web.Controls" tagprefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <asp:PlaceHolder ID="PlaceHolder1" runat="server">        
        <%: Styles.Render("~/bundles/Datatables/css") %>
        <%: Scripts.Render("~/bundles/Datatables") %>
        <%: Styles.Render("~/bundles/Formly/css") %>
        <%: Scripts.Render("~/bundles/Formly") %>
    </asp:PlaceHolder>

    <script type="text/javascript" lang="javascript" src="../Scripts/Formly/FormlyHelper.js"></script>
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.Core.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.Directives.Fileupload.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.Directives.DateTimePicker.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.DataTables.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.Modals.js"></script> 
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <section class="panel">
<%--	    <header class="panel-heading">
			<b>Anagrafica Articoli</b>
		</header>--%>

<%--        <a class="collapsed" data-toggle="collapse" href="#collapseSetting">
            <h4 class="fa  fa-search"> Filters</h4>
        </a>
        <section id="collapseSetting" class="collapse" style="height: 0px;">
            <div class="collapse-boby" style="padding:0">
                <% HttpContext.Current.Response.Write(this.JQDTable.RenderExternalFilters()); %>
            </div>
        </section>--%>

		<div class="panel-body">
            <div class="row">
			  <div class="col-md-6">
            <!-- PUT YOUR CONTENT HERE -->
                <iframe id='iframeCrud' frameborder="0" style="width:400px; height:300px; display:none"></iframe>
                <div id="crudDialog" title="" style="display:none"></div>
                <div id="deleteDiv" title="" style="display:none"></div>

                <div ng-app="formlyExample" ng-controller="MainCtrl as vm" name="divContainer" id="div1">
                    <div ng-if="vm.loadingData.$$state.status === 0" style="margin:20px 0;font-size:2em">
                        <strong>Caricamento...</strong>
                    </div>
                    <div ng-if="vm.loadingData.$$status.state !== 0">
                        <form ng-submit="vm.onSubmit()" novalidate id="form1">
                            <formly-form model="vm.model" fields="vm.fields" form="vm.form">
                            </formly-form>
                        </form>
                    </div>
                </div>

                <cc1:JQDataTable ID="JQDTable" runat="server" 
                        SelectionMode="Single" 
                        RenderScripts="false" 
                        BootstrapClasses="table table-hover table-bordered table-condensed compact">
                    <FeaturesConfig>
                        <Features>
                            <cc1:Searching Value="General">
                                <Filters>
                                    <cc1:ColumnFilter ColumnName="Codice" Type="Text" />
                                </Filters>
                            </cc1:Searching>
                            <cc1:AutoWitdh Value="true" />
                            <cc1:ScrollY Value="500" ScrollCollapse="true" />
                            <cc1:LengthChange Value="false" />
                            <cc1:Paging PagingType="FullNumbers" PageLength="20" />
                        </Features>
                    </FeaturesConfig>
                    <ControllerConfig Url="/api/AnagraficaCodiciFornitori/LoadByFornitore" LoadingMode="OnDemand">
                        <Validation Message="Inserire i parametri" FormId="form1" />
                        <ControllerParameters>
                            <cc1:ControllerParameter Name="formly_1_asyncselect_Fornitori_0" Alias="Fornitore" />
                        </ControllerParameters>
                    </ControllerConfig>
                    <ColumnsConfig Key="Id">
                        <Columns>
                            <cc1:TextColumn Name="Id" Header="Id" Visible="false" />
                            <cc1:TextColumn Name="Codice" Header="Codice" Width="250" />
                            <cc1:TextColumn Name="FCD_Customer.RagioneSociale" Header="Magazzino" Width="250" />
                            <cc1:ActionColumn Header="Azioni" >
                                <Actions>
                                    <cc1:ActionIcon Icon="fa fa-pencil" Callback="EditCallback" />
                                    <cc1:ActionIcon Icon="fa fa-trash-o" Callback="DeleteCallback" />
                                    <%--<cc1:ActionIcon Icon="fa fa-search" Callback="ShowDetails" />--%>
                                </Actions>
                            </cc1:ActionColumn>
                        </Columns>
                    </ColumnsConfig>
                    <Buttons>
                        <cc1:CrudAddButton Text="Aggiungi" Callback="LoadAddDialog" />
                    </Buttons>
                </cc1:JQDataTable>

              </div>
            </div>
         </div>                      
	</section>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="LocaJavascript" runat="server">
    <% HttpContext.Current.Response.Write(this.JQDTable.RenderScriptsManually()); %>

<script>
    $(document).ready(function () {
        // BOOTSTRAP: Tweaks
        $('.yadcf-filter-wrapper').addClass('input-group');
        $('.yadcf-filter, .yadcf-filter-date', this).addClass('form-control input-sm');
        $('.yadcf-filter-reset-button', this).addClass('btn btn-default btn-sm').wrap('<span class="input-group-btn"></span>');
    });

    var settings = new FormlySettings();
    settings.container = '#divFormly';
    settings.setLoad(settings.createGetConfig('/api/AnagraficaCodiciFornitori/LoadFornitoriFields'));
    FormlyManager(settings);

    function CloseDialog(response) {
        $('#crudDialog').dialog('close');

        ShowResponseMessage(response);
    }

    function ShowDetails(obj) {
        var p = null;
        p = obj.Id;

        var url = '/api/AnagraficaCodiciFornitori/GetArticoli';

        $.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify({ id: p }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                RedrawGrid(m_tableJQDTDettaglio);
            },
            failure: function (response) {
                alert(response);
            }
        });
    }

    function LoadAddDialog() {

        var m_id_fornitore = $('#formly_1_asyncselect_Fornitori_0').val();
        m_id_fornitore = m_id_fornitore.replace('string:', '');

        $("#iframeCrud").prop('src', 'AnagraficaCodiciFornitori_CRUD.aspx?idMg=' + m_id_fornitore).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Anagrafica Codici Fornitori',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function () {
                RedrawGrid(m_tableJQDTable);

                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function EditCallback(obj) {
        var p = null;
        p = obj.Id;

        var m_id_fornitore = $('#formly_1_asyncselect_Fornitori_0').val();
        m_id_fornitore = m_id_fornitore.replace('string:', '');

        var url = 'AnagraficaCodiciFornitori_CRUD.aspx?id=' + p + '&idMg=' + m_id_fornitore;

        $("#iframeCrud").prop('src', url).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Anagrafica Codici Fornitori',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function () {
                RedrawGrid(m_tableJQDTable);

                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function DeleteCallback(obj) {
        var p = null;
        p = obj.Id;

        var url = '/api/AnagraficaCodiciFornitori/Delete';

        $("#deleteDiv").contents().remove();
        $("#deleteDiv").html("Confermi la cancellazione dell'elemento con codice: " + obj.Codice + " ?").appendTo("#crudDialog");
        //$("#crudDialog").html("Confermi la cancellazione dell'elemento con codice: " + p + " ?");
        $("#deleteDiv").show();

        // Define the Dialog and its properties.
        $("#crudDialog").dialog({
            resizable: false,
            modal: true,
            title: "Cancella elemento",
            height: 250,
            width: 400,
            buttons:[
                {
                    id: "Yes",
                    text: "Si",
                    click: function () {
                        $(this).dialog("option", { buttons: {} });
                        //$(this).empty();
                        $(this).dialog('close');

                        $.ajax({
                            type: "POST",
                            url: url,
                            data: JSON.stringify({ id: p }),
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                RedrawGrid(m_tableJQDTable);

                                //$("#crudDialog").html('');
                                $("#deleteDiv").contents().remove();
                                $('#deleteDiv').hide();

                                alert(response);
                            },
                            failure: function (response) {

                                //$("#crudDialog").empty();
                                $("#deleteDiv").contents().remove();
                                $('#deleteDiv').hide();

                                alert(response);
                            }
                        });
                    }
                },
                {
                    id: "No",
                    text: "No",
                    click: function () {
                        //$("#crudDialog").empty();
                        $("#deleteDiv").contents().remove();
                        $('#deleteDiv').hide();

                        $(this).dialog('close');
                    }
                }
            ]
        });
    }

    function LoadEditDialog() {

        var p = null;
        p = GetSelectedData(m_tableJQDTable).Id;

        var m_id_magazzino = $('#formly_1_asyncselect_selMagazzini_0').val();
        m_id_magazzino = m_id_magazzino.replace('string:', '');
               
        var url = 'AnagraficaArticoli_CRUD.aspx?id=' + p + '&idMg=' + m_id_magazzino;

        $("#iframeCrud").prop('src', url).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Anagrafica Articoli',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function () {
                RedrawGrid(m_tableJQDTable);

                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function DeleteDialog() {
        var p = null;

        var row = GetSelectedData(m_tableJQDTable);

        p = row.Id;

        var url = '/api/AnagraficaArticoli/Delete';

        $("#deleteDiv").contents().remove();
        $("#deleteDiv").html("Confermi la cancellazione dell'elemento con codice: " + p + " ?").appendTo("#crudDialog");
        //$("#crudDialog").html("Confermi la cancellazione dell'elemento con codice: " + p + " ?");
        $("#deleteDiv").show();

        // Define the Dialog and its properties.
        $("#crudDialog").dialog({
            resizable: false,
            modal: true,
            title: "Cancella elemento",
            height: 250,
            width: 400,
            buttons:[
                {
                    id: "Yes",
                    text: "Si",
                    click: function () {
                        $(this).dialog("option", { buttons: {} });
                        //$(this).empty();
                        $(this).dialog('close');

                        $.ajax({
                            type: "POST",
                            url: url,
                            data: JSON.stringify({ id: p }),
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                RedrawGrid(m_tableJQDTable);

                                //$("#crudDialog").html('');
                                $("#deleteDiv").contents().remove();
                                $('#deleteDiv').hide();

                                alert(response);
                            },
                            failure: function (response) {

                                //$("#crudDialog").empty();
                                $("#deleteDiv").contents().remove();
                                $('#deleteDiv').hide();

                                alert(response);
                            }
                        });
                    }
                },
                {
                    id: "No",
                    text: "No",
                    click: function () {
                        //$("#crudDialog").empty();
                        $("#deleteDiv").contents().remove();
                        $('#deleteDiv').hide();

                        $(this).dialog('close');
                    }
                }
            ]
        });
    }
</script>
</asp:Content>